//
//  UIViewController+ImagePicker.swift
//  AJiTai
//
//  Created by liufengting on 2020/5/24.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import UIKit
import TZImagePickerController

public extension UIViewController {
    
    // 图片单选
    func pickSinglePicture(allowCrop: Bool = false, cropRect: CGRect = CGRect.zero, completion: ((UIImage?) ->())?) {
        let imagePicker = TZImagePickerController(maxImagesCount: 1, delegate: nil)
        imagePicker?.allowPickingOriginalPhoto = true
        imagePicker?.allowPickingVideo = false
        imagePicker?.allowTakePicture = true
        imagePicker?.allowPickingGif = false
        imagePicker?.allowCrop = allowCrop
        if allowCrop {
            if cropRect == CGRect.zero {
                let left: CGFloat = 30.0
                let widthHeight: CGFloat = SCREEN_WIDTH - (2.0*left)
                let top: CGFloat = (SCREEN_HEIGHT - NAV_BAR_HEIGHT - widthHeight)/2.0
                imagePicker?.cropRect = CGRect(x: left, y: top, width: widthHeight, height: widthHeight)
            } else {
                imagePicker?.cropRect = cropRect
            }
            imagePicker?.showSelectBtn = false
        }
        imagePicker?.didFinishPickingPhotosHandle = {(photos, assets, isSelectOriginalPhoto) in
            if let imageArray: Array<UIImage> = photos {
                completion?(imageArray.first)
            }
        }
        if let vc = imagePicker {
            self.present(vc, animated: true) {
                
            }
        }
    }
    
    // 图片多选
    func pickMultiPicture(count: NSInteger, completion: ((Array<UIImage>?) ->())?) {
        let imagePicker = TZImagePickerController(maxImagesCount: count, delegate: nil)
        imagePicker?.allowPickingOriginalPhoto = true
        imagePicker?.allowPickingVideo = false
        imagePicker?.allowTakePicture = true
        imagePicker?.allowPickingGif = false
        imagePicker?.didFinishPickingPhotosHandle = {(photos, assets, isSelectOriginalPhoto) in
            completion?(photos)
        }
        if let vc = imagePicker {
            self.present(vc, animated: true) {
                
            }
        }
    }
    
}


